import wx
import datetime
from wx.lib.pubsub import setuparg1
from wx.lib.pubsub import pub


import SimplePanels
from SimplePanels import PanelRecord
from ViewBaseList import ViewBaseList
from ModelRecord import ModelRecord
from DataManager import DataManager


class ViewRecordList(wx.Panel):

    fields = ('type', 'start','end','rating','mood')
    widths = (50,150,150,50,50)
    col_cmper = (lambda x, y: cmp(x[0], y[0]),
                 lambda x, y: cmp(x[1], y[1]),
                 lambda x, y: cmp(x[2], y[2]),
                 lambda x, y: cmp(x[3], y[3]),
                 lambda x, y: cmp(x[4], y[4]))

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.init_widgets()
        self.init_control()
        self.init_data()

    def init_data(self):
        """
        we still need to have a copy of these things in memory to handle sorting
        """
        self.mode = SimplePanels.PANEL_VIEW_TIME_DAY #this is a invalid value
        self.ref = datetime.date.today()
        self.order_of_colomn = [0] * len(self.fields)
        #self.data_list = []
        self.data_update()

    def init_widgets(self):
        """
        need a sizer?
        only ListCtrl I believe
        """
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.ui_list = wx.ListCtrl(self, style = wx.LC_REPORT)
        for i, j in enumerate(self.fields):
            self.ui_list.InsertColumn(i, j)
            self.ui_list.SetColumnWidth(i, self.widths[i])

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.ui_list, 1, wx.EXPAND)
        self.SetSizer(sizer)

    def init_control(self):
        """
        subscribe
        and bind dealing with ui level effects
        """
        self.Bind(wx.EVT_LIST_COL_CLICK, self.on_list_column_click)
        pub.subscribe(self.on_sync_done, ModelRecord.M_REFRESH)
        

    def on_list_column_click(self, evt):
        col = evt.GetColumn()
        if col < 0 or col >= len(self.fields):
            #get lost
            return

        self.data_list.sort(self.col_cmper[col], reverse=self.order_of_colomn[col])
        #toggle this
        self.order_of_colomn[col] = not self.order_of_colomn[col]
        #once data_list is changed, ui_list must be changed, even if it's just sorting
        #we keep this data_list only to make sorting easier

        #this is the only case when ui_update is directly called, none other!
        self.ui_update()

    def on_sync_done(self, m):
        self.data_update()

    def ui_update(self):
        """
        clean all, redo everything, used when data is updated(for example,
        synced with remote server)
        """
        #NOTE: data_update will call ui_update automaticlly
        self.ui_list.DeleteAllItems()
        for i in self.data_list:
            p = self.ui_list.Append(i)
            self.ui_list.SetItemData(p, p)

    def data_update(self):
        """
        deal with database, querry what we need
        Model class shouldn't have any storage
        """
        #record model is special here because it changes nothing
        self.data_list = DataManager.record.data

        #call ui_update in the end
        self.ui_update()


class TestClass(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        sizer = wx.BoxSizer(wx.VERTICAL)
        p = ViewRecordList(self)
        sizer.Add(p, 1, wx.EXPAND)
        self.SetSizer(sizer)
        self.Show()

if __name__ == '__main__':
    import DataManager
    DataManager.DataManager.init()
    app = wx.App()
    f = TestClass()
    app.MainLoop()
